/**
 * Project Name:fastPlat
 * File Name:SystemScheduledImpl.java
 * Package Name:com.fast.service.impl
 * Date:2015年8月1日上午10:54:35
 * Copyright (c) 2015, lsflu@126.com All Rights Reserved.
 *
*/

package com.fast.task.scheduled;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.fast.base.dao.JdbcDao;
import com.fast.base.dao.MybatisDao;
import com.fast.base.util.LogUtils;
import com.fast.base.util.data.MathUtils;
import com.fast.cms.model.article.BatchDataInfo;
import com.fast.cms.model.article.MdData;
import com.fast.cms.service.article.MdDataService;
import com.fast.cms.service.system.DictionaryService;
import com.fast.cms.web.form.MdDataForm;
import com.fast.user.model.admin.Permission;

/**
 * ClassName:SystemScheduledImpl <br/>
 * 作用: 系统任务调试 <br/>
 * Date:     2015年8月1日 上午10:54:35 <br/>
 * @author   Administrator
 * @version  
 * @since    JDK 1.6
 * @see 	 
 */
@Component("systemScheduledTask")
public class SystemScheduledTask{
	
	@Resource(name="dictionaryService")
	private DictionaryService dictionaryService;
	
	@Autowired
	private MybatisDao mybatisDao01;
	
	@Autowired
	private JdbcDao jdbcDao01;
	
	@Autowired
	private MdDataService mdDataService;
	
	//系统运行日志操作文件
	protected final static Log log = LogFactory.getLog(SystemScheduledTask.class);
	
    /**
     * 标题: 更新批次的总数量 <br>
     * 编写者:lushuifa
     * 创建时间:2018年9月3日 上午12:15:49 </pre>
     * @param
     * @return 
     * @see com.fast.task.scheduled.SystemScheduled#updateBatchCountForMdBatch()
     */
	@Scheduled(cron="0/5 * * * * ?") 
	public void updateBatchCountForMdBatch() {
	   this.mybatisDao01.getSession().update("mdData.updateBatchOneMdData");
	   this.mybatisDao01.getSession().delete("mdData.deleteBatchOneMdData");
	   List<String> batchNoList = this.mybatisDao01.getSession().selectList("mdData.getBatchDataInfoByBatchCountIsNull");
	   if (!CollectionUtils.isEmpty(batchNoList)) {
		   for (String batchNo : batchNoList) {
			   int updateCount = this.mybatisDao01.getSession().update("mdData.updateBatchDataInfoByBatchCountIsNull", batchNo);
		       LogUtils.info("updateBatchCountForMdBatch updateCount:"+updateCount);
		   }
	   }
	}
	
	@Scheduled(cron="0/30 * * * * ?") 
	public void reloadMianganCi() {
		int page = 1; 
		while (true) {
			MdDataForm mdDataForm = new MdDataForm();
			mdDataForm.setDataType("simple");
			mdDataForm.setPage(page);
			mdDataForm.setRows(300);
			mdDataForm.setStartIndex((page-1)*mdDataForm.getRows());
			List<MdData> batchDataList = this.mybatisDao01.getSession().selectList("mdData.getMdDataList", mdDataForm   );
			if (CollectionUtils.isEmpty(batchDataList)) {
				break;
			}
			for (MdData mdData : batchDataList) {
				mdDataService.fileterPingPaiGanCi(mdData.getUuid(), mdData.getDataTitle());
				mdDataService.fileterTitleGanCi(mdData.getUuid(), mdData.getDataTitle());
			}
			page++;
		}
		
	}
	
	
	//@Scheduled(cron="0/5 * * * * ?") 
	public void supplementBatchData(){
		List<Map<String, Object>> batchDataList = mybatisDao01.getSession().selectList("mdData.queryNotEnough300Data");
		if (!CollectionUtils.isEmpty(batchDataList)) {
			for (Map<String, Object> dataMap : batchDataList) {
				String batchNo = (String) dataMap.get("batchNo");
				int count = (Integer) dataMap.get("batchCount");
				int diffCount = 300-count;
				List<Map<String, Object>> mdDataList = mybatisDao01.getSession().selectList("mdData.queryMdDataCountByGroup",diffCount);
				if (!CollectionUtils.isEmpty(mdDataList)) {
					for (Map<String, Object> mdData : mdDataList) {
						int id = (Integer) mdData.get("id");
						Map<String, Object> paramMap = new HashMap<String,Object>();
						paramMap.put("batchNo",batchNo);
						paramMap.put("id",id);
						int updateCount = mybatisDao01.getSession().update("mdData.updateMdDataBatchNoById",paramMap);
						LogUtils.info("updateCount:"+updateCount);
					}
				}
			}
		}
	}
	
	//@Scheduled(cron="0/60 * * * * ?") 
	public void generalBatchDataList() {
		BatchDataInfo batchDataInfo = null;
		int index = 1;
		while (true) {
			List<Map<String, Object>> mdDataList = mybatisDao01.getSession().selectList("mdData.queryMdDataCountByGroup",300);
			if (CollectionUtils.isEmpty(mdDataList)) {
				break;
			}
			LogUtils.info("mdDataList["+index+"]:"+mdDataList);
			//生成批次号
			String batchNo = MathUtils.getTheOnlyStr();
			String dataUrl = "";
			for (Map<String, Object> mdData : mdDataList) {
				int id = (Integer) mdData.get("id");
				dataUrl = (String) mdData.get("dataUrl");
				Map<String, Object> paramMap = new HashMap<String,Object>();
				paramMap.put("batchNo",batchNo);
				paramMap.put("id",id);
				int updateCount = mybatisDao01.getSession().update("mdData.updateMdDataBatchNoById",paramMap);
				LogUtils.info("updateCount:"+updateCount);
			}
			batchDataInfo = new BatchDataInfo();
			batchDataInfo.setUuid(batchNo);
			batchDataInfo.setBatchDesc("第【"+index+"】个店铺");
			batchDataInfo.setCreater("系统机器人");
			batchDataInfo.setCreateTime(new Date());
			batchDataInfo.setDataType("");
			batchDataInfo.setDeleteFlag(0);
			batchDataInfo.setEditorName("");
			batchDataInfo.setEditTime(new Date());
			batchDataInfo.setFileName("batch_"+batchNo+".txt");
			batchDataInfo.setIsDownload(0);
			batchDataInfo.setBatchCount(0);
			batchDataInfo.setUrlId(dataUrl);
			jdbcDao01.saveOrModify(batchDataInfo, "where uuid='"+batchNo+"'");
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			LogUtils.info("剩余数量：第【"+index+"】个店铺");
			index++;
		}
		
	}
	
	@Scheduled(cron="0/30 * * * * ?") 
	public void repeatCheckMianGanCi() {
		List<Map<String, Object>> dataList = this.mybatisDao01.getSession().selectList("mdData.getMianganList");
		for (Map<String, Object> dataMap : dataList) {
			String uuid = (String) dataMap.get("uuid");
			String dataTitle = (String) dataMap.get("dataTitle");
			mdDataService.fileterPingPaiGanCi(uuid, dataTitle);
			mdDataService.fileterTitleGanCi(uuid, dataTitle);
		}
		this.mdDataService.supplementBatchData();
	}
	
	/**
	 * 说明:批量删除无用权限值<br>
	 * 编写者:lushuifa
	 * 创建时间:2017年5月7日 上午12:01:12 </pre>
	 * @param 
	 * @return void 说明
	 */
	@Scheduled(cron="0 0/20 * * * ?") 
	public void batchDeletePermission(){
		String sql = "SELECT\n" +
				"	t3.uuid\n" +
				"FROM\n" +
				"	(\n" +
				"		SELECT\n" +
				"			t1.uuid,\n" +
				"			t1.psmName,\n" +
				"			t2.uuid AS parentUUID\n" +
				"		FROM\n" +
				"			t_dv_permission t1\n" +
				"		LEFT JOIN t_dv_permission t2 ON t1.parentUUID = t2.uuid\n" +
				"		WHERE\n" +
				"			t1.parentUUID <> '0'\n" +
				"	) t3 where t3.parentUUID is null";
	     
		//log.info(sql);
		
		List<Map<String,Object>> mapList = this.dictionaryService.queryForMapList(sql);
		if(null!=mapList&&!mapList.isEmpty()){
			for (Map<String, Object> map : mapList) {
				String uuid = (String) map.get("uuid");
				boolean actionFlag = this.dictionaryService.deleteByUUID(Permission.class, uuid);
				log.info("删除状态为："+actionFlag);
			}
		}
	}
	
	
	/**
	 * 说明:更新权限与父类名字不一致问题<br>
	 * 编写者:lushuifa
	 * 创建时间:2017年5月7日 上午12:01:12 </pre>
	 * @param 
	 * @return void 说明
	 */
	@Scheduled(cron="0 0/20 * * * ?") 
	public void batchUpdatePermission(){
		String sql = "SELECT\n" +
				"  t1.uuid,\n" +
				"	t1.psmName,\n" +
				"	t1.parentName,\n" +
				"	t2.psmName AS parentName2\n" +
				"FROM\n" +
				"	t_dv_permission t1\n" +
				"INNER JOIN t_dv_permission t2 ON t1.parentUUID = t2.uuid\n" +
				"where t1.parentName<>t2.psmName";
		
		List<Map<String,Object>> mapList = this.dictionaryService.queryForMapList(sql);
		if(null!=mapList&&!mapList.isEmpty()){
			for (Map<String, Object> map : mapList) {
				String uuid = (String) map.get("uuid");
				String parentName2 = (String) map.get("parentName2");
				String sql2 = "update t_dv_permission t1 set t1.parentName='"+parentName2+"' where t1.uuid='"+uuid+"'";
				this.dictionaryService.execute(sql2);
				log.info("已执行："+sql2);
			}
		}
	}
}

